#!/bin/bash
#Name:discuz.sh
#Desc:安装适合discuz博客版本的lamp和依赖
#path:/root/discuz.sh
#Usage:./discuz.sh
#Update:2024-07-04
rpm -qa | grep bash-completion
if [ ! $? -eq 0  ]
then 
	yum install bash-completion -y
fi
rpm -qa | grep vim
if [ ! $? -eq 0 ]
then
	yum install  vim -y
fi
yumrepo(){
	tar -zcf /etc/yum.repos.d/localrepo-$(date +\%F--\%T).tar.gz /etc/yum.repos.d/*
	rm -rf /etc/yum.repos.d/*.repo
	rpm -qa | grep wget
	if [ ! $? -eq 0]
	then
		yum install wget -y
	fi
	wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
	wget -O /etc/yum.repos.d/CentOS-Base-aliyun.repo https://mirrors.aliyun.com/repo/Centos-7.repo
}
read -p "是否替换为阿里云的yum源和epel，原来的yum源将被备份:[y/n] " repo
case $repo in
y|Y|yes|Yes|YEs|YeS|YES|yeS|yEs|yES)
	yumrepo
;;
*)
	echo "您没有替换yum源"
;;
esac
grep -Rl 'epel' /etc/yum.repos.d/ &>/dev/null
if [ ! $? -eq 0 ]   
then
	yum install epel-release
fi
#安装Remi仓库：
#Remi仓库提供了最新版本的PHP和其他软件包
rpm -qa | grep remi
if [ $? -eq 0 ]
then
	yum remove  remi-release.noarch -y
	yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
else 
	yum install	https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
fi
#启用Remi仓库中的PHP 7.4模块
	yum-config-manager --enable remi-php74
#重新建立缓存
	yum clean all
	yum makecache



setenforce 0
sed -ri '/^SELINUX/c SELINUX=disabled' /etc/selinux/config

server=("httpd" "php" "mysql")
fun(){
rpm -qa | grep $1
if [ $? -ne 0 ]
then
        if [[ "$1"="httpd" ]]
        then
                yum install httpd -y
		else
				yum remove httpd -y
				yum install httpd -y
        fi
        if [[ "$1"="php" ]]
        then
                yum install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json -y
		else 	
				yum remove php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json -y
        fi
        if [[ "$1"="mysql" ]]
        then
                yum install mariadb-server mariadb -y
		else 	
				yum remove mariadb-libs.x86_64  mariadb-server.x86_64  mariadb.x86_64
				yum install mariadb-server mariadb -y
        fi
fi
}

for ((i=0;i<${#server[@]};i++))
do

        fun "${server[$i]}"
        echo "${server[$i]}"
        echo "$i"
done

	rm -rf /etc/httpd/conf.d/welcome.conf
	systemctl start httpd
	systemctl enable httpd

	systemctl start mariadb
	systemctl enable mariadb


Mysql_Pass=root
	mysqladmin -uroot password "$Mysql_Pass"
	mysql -uroot -p"$Mysql_Pass" -e "create database  discuz"

if [ ! -e /root/download  ]
then
        wget https://gitee.com/Discuz/DiscuzX/attach_files/1309965/download
fi

	yum  install  -y   unzip

if [ ! -e upload ]
then
        unzip  download
fi

	rm -rf /var/www/html/*
	cp -rf upload/* /var/www/html
	chown -R  apache.apache  /var/www/html

	systemctl restart httpd mariadb

firewall-cmd --list-all | grep http
if [ ! $? -eq 0  ]
then
        firewall-cmd --permanent --zone=public --add-service=http
fi
firewall-cmd --list-all | grep mysql
if [ ! $? -eq 0  ]
then
        firewall-cmd --permanent --zone=public --add-service=mysql
fi
firewall-cmd --reload


